package com.qch.rabbitmq.demo;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author qch
 * @Date 2021/5/14
 */
public class ProducerApp {
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RabbitConfig.class);
        RabbitTemplate template = applicationContext.getBean(RabbitTemplate.class);
       // Message msg;
        MessageProperties messageProperties =
                MessagePropertiesBuilder.newInstance().
                        setContentEncoding("gbk").
                        setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN).
                        setHeader("mykey", "myvalue").build();
       /* msg = MessageBuilder.withBody("你好".getBytes("gbk"))
                .andProperties(messageProperties).build();
        //在spring-rabbit.xml中配置路由键等
        template.send("ex.anno.fanout","key.anno",msg);*/
        for (int i = 0; i < 100; i++) {
            Message msg = MessageBuilder.withBody(("你好"+i).getBytes("gbk"))
                    .andProperties(messageProperties).build();
            //在spring-rabbit.xml中配置路由键等
            template.send("ex.anno.fanout","key.anno",msg);
        }

((AnnotationConfigApplicationContext) applicationContext).close();
    }
}
